﻿package com.mycompany.action;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.mycompany.util.ConstantUtil;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport 
				implements ServletRequestAware, SessionAware
{
	private File data;
	private String dataContentType;
	private String dataFileName;
	private String destPage;
	
	private HttpServletRequest servletRequest;
	private Map<String, Object> session;
	
	@Override
	public String execute()
	{
		try
		{
			String filePath = servletRequest.getSession()
								.getServletContext().getRealPath("/upload/");
			String newFileName = System.currentTimeMillis() + this.dataFileName;
			File fileToCreate = new File(filePath, newFileName);
			FileUtils.copyFile(this.data, fileToCreate);
			session.put(ConstantUtil.CURRENT_FILE, "../upload/" + newFileName);
		} // end try
		catch (Exception e)
		{
			e.printStackTrace();
		} // end catch
		
		
		return SUCCESS;
	} // end execute()
	
	
	
	public String getDestPage() {
		return destPage;
	}



	public void setDestPage(String destPage) {
		this.destPage = destPage;
	}



	public File getData() {
		return data;
	}
	public void setData(File data) {
		this.data = data;
	}
	public String getDataContentType() {
		return dataContentType;
	}
	public void setDataContentType(String dataContentType) {
		this.dataContentType = dataContentType;
	}
	public String getDataFileName() {
		return dataFileName;
	}
	public void setDataFileName(String dataFileName) {
		this.dataFileName = dataFileName;
	}



	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest = request;
	}



	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	

}
